//
//  HZDropDownMenu.m
//  ListMenu
//
//  Created by hzbj on 15/11/16.
//  Copyright © 2015年 hzbj. All rights reserved.
//

#import "HZDropDownMenu.h"

@interface HZDropDownMenu ()

/** 用来显示具体内容的容器 */
@property (nonatomic, strong) UIView *containerView;


@end


@implementation HZDropDownMenu

- (UIView *)containerView {
    
    if (!_containerView) {
        self.containerView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
        self.containerView.backgroundColor = [UIColor grayColor];
        [self addSubview:self.containerView];
    }
    return _containerView;
}


- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        // 清除颜色
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

+ (instancetype)menu {
    
    return [[self alloc] init];
}

/** set方法 */
- (void)setContent:(UIView *)content {
    
    _content = content;
    [self.containerView addSubview:content];
    self.containerView.frame = CGRectMake(0, 0, CGRectGetMaxX(_content.frame), CGRectGetMaxY(_content.frame));
    
}

/** 显示 */
- (void)showFrom:(UIView *)from {
    
    // 获得最上面的窗口
    UIWindow * window = [[UIApplication sharedApplication].windows lastObject];
    // 添加自己到窗口上
    [window addSubview:self];
    // 设置尺寸
    self.frame = window.bounds;
    // 调整containerView的位置
    // 默认情况下，frame是以父控件左上角为坐标原点
    // 可以转换坐标系原点，改变frame的参照点
    CGRect newFrame = [from.superview convertRect:from.frame toView:window];
    
   
    // containerView的中心点坐标
    self.containerView.center = CGPointMake(CGRectGetMidX(newFrame), self.containerView.frame.size.height / 2 + CGRectGetMaxY(newFrame) + 30);
    
}



- (void)showFromWithBarButton:(UIBarButtonItem *)from {
    /*
    // 获得最上面的窗口
    UIWindow * window = [[UIApplication sharedApplication].windows lastObject];
    // 添加自己到窗口上
    [window addSubview:self];
    // 设置尺寸
    self.frame = window.bounds;
    // 调整containerView的位置
    // 默认情况下，frame是以父控件左上角为坐标原点
    // 可以转换坐标系原点，改变frame的参照点
    CGRect newFrame = [from.i convertRect:from.frame toView:window];
    
    
    // containerView的中心点坐标
    self.containerView.center = CGPointMake(CGRectGetMidX(newFrame), self.containerView.frame.size.height / 2 + CGRectGetMaxY(newFrame));
     */

}






/** 销毁 */
- (void)dismiss {
    
    [self removeFromSuperview];
}
/** 给self添加touchesBegan销毁视图 */
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    [self dismiss];
    
}

@end
